package com.online.dao.user.Impl;

import com.online.dao.user.billDao;
import com.online.pojo.Bill;
import com.online.pojo.menu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class billDaoImpl implements billDao {

    public List<Bill> select( List<Bill> list, String user_name) throws SQLException {

        ResultSet rs;

        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.bill where user_name=? and pay=2";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1, user_name);
        rs= pstm.executeQuery();
        int i=0;
        while (rs.next()) {
            Bill bill=new Bill();
            bill.setShop_name(rs.getString(1));
            bill.setShopid(rs.getString(2));
            bill.setUser_name(rs.getString(3));
            bill.setPrice(rs.getDouble(4));
            bill.setPay(rs.getInt(5));
            bill.setId(rs.getInt(6));
            list.add(i,bill);
            i++;
        }
        pstm.close();
        connection.close();
        return list;
    }

    public boolean del(int id) throws SQLException {
        boolean falg;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql ="delete from online.bill where id=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setInt(1,id);
        rs=pstm.executeUpdate();
        if(rs>0){
            falg=true;
            System.out.println("已删除");
        }else {
            falg=false;
        }
        pstm.close();
        connection.close();
        return falg;
    }

    public boolean update(String user_name) throws SQLException {
        boolean falg=false;
        int rs;
        Connection connection = BaseDao.getConnection();
        String sql = "update online.bill set pay=1 where pay=2 and user_name=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,user_name);
        rs=pstm.executeUpdate();
        if(rs>0) {
            falg=true;
            System.out.println("更新成功");
        }
        pstm.close();
        connection.close();
        return falg;
    }

    public List<Bill> selectBilled(List<Bill> list, String user_name) throws SQLException {
        ResultSet rs;

        Connection connection = BaseDao.getConnection();
        String sql ="select * from online.bill where pay=1 and user_name=?";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1, user_name);
        rs= pstm.executeQuery();
        int i=0;
        while (rs.next()) {
            Bill bill=new Bill();
            bill.setShop_name(rs.getString(1));
            bill.setShopid(rs.getString(2));
            bill.setUser_name(rs.getString(3));
            bill.setPrice(rs.getDouble(4));
            bill.setPay(rs.getInt(5));
            bill.setId(rs.getInt(6));
            list.add(i,bill);
            i++;
        }
        System.out.println(list);
        pstm.close();
        connection.close();
        return list;
    }
}
